package com.sihan.framework.etm.controller.business;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.framework.annotation.BusinessLog;
import com.framework.model.ApiResult;
import com.sihan.framework.etm.common.CacheOperatorInfo;
import com.sihan.framework.etm.common.CommonBiz;
import com.sihan.framework.etm.dto.CommonQueryDTO;
import com.sihan.framework.etm.dto.NameAndTypeDTO;
import com.sihan.framework.etm.dto.business.ToolBorrowReturnRecordDTO;
import com.sihan.framework.etm.dto.business.ToolInWarehouseMasterDTO;
import com.sihan.framework.etm.dto.business.ToolPurchaseDTO;
import com.sihan.framework.etm.dto.business.ToolPurchaseDetailDTO;
import com.sihan.framework.etm.enums.CacheKeyName;
import com.sihan.framework.etm.enums.MqttSendCommandFunc;
import com.sihan.framework.etm.service.business.ToolPurchaseService;
import com.sihan.framework.etm.vo.CommonQueryResultVO;
import com.sihan.framework.etm.vo.business.PurchasePlanDetailVO;
import com.sihan.framework.etm.vo.business.PurchasePlanMasterVO;
import com.sihan.framework.etm.vo.business.ToolInWarehouseDetailVO;
import com.sihan.framework.etm.vo.business.ToolInWarehouseStandingBookVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = "采购管理")
@RestController
@RequestMapping("/bs/v0/purchase")
public class PurchaseController {
	
	@Autowired
	private ToolPurchaseService toolPurchaseService;
	
	@Autowired
	private CommonBiz commonBiz;

	@ApiOperation(value = "采购计划列表", notes = "采购计划列表", response = PurchasePlanMasterVO.class)
	@GetMapping("/plan")
	@BusinessLog(value = "采购计划列表")
	@ApiImplicitParams({
	      @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
	      @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true),
	      @ApiImplicitParam(name = "orderBy", value = "排序字段", dataType = "string", paramType = "query"),
	      @ApiImplicitParam(name = "queryKeywords", value = "计划名称", dataType = "string", paramType = "query"),
	      @ApiImplicitParam(name = "queryStartTime", value = "查询开始检", dataType = "string", paramType = "query"),
	      @ApiImplicitParam(name = "queryEndTime", value = "查询结束时间", dataType = "string", paramType = "query")
	  })
	@RequiresPermissions("bs:tool:purchase:plan")
	public ApiResult<?> listPurchasePlanMaster(ToolPurchaseDTO dto){
		return ApiResult.success(toolPurchaseService.selectPage(dto));
	}
	
	@ApiOperation(value = "计划提交", notes = "计划提交", response = ApiResult.class)
	@GetMapping("/plan/submit")
	@BusinessLog(value = "计划提交")
	public ApiResult<?> submitPlan(String id){
		toolPurchaseService.submitPlan(id);
		return ApiResult.success();
	}
	
	@ApiOperation(value = "采购入库时间", notes = "采购入库时间", response = ApiResult.class)
	@GetMapping("/plan/warehouse")
	@BusinessLog(value = "采购入库时间")
	public ApiResult<?> submitWarehouse(String id,String warehouseTime){
		toolPurchaseService.purchaseInWarehouse(id, warehouseTime);
		return ApiResult.success();
	}
	
	@ApiOperation(value = "采购计划明细", notes = "采购计划明细", response = PurchasePlanDetailVO.class)
	@GetMapping("/plan/details")
	@BusinessLog(value = "采购计划明细")
	@ApiImplicitParams({
	      @ApiImplicitParam(name = "typeModel", value = "类型型号", dataType = "string", paramType = "query"),
	  })
	public ApiResult<?> listPurchasePlanDetail(ToolPurchaseDTO dto){
		return ApiResult.success(toolPurchaseService.listPlanDetailByPlanId(dto));
	}
	
	@ApiOperation(value = "新增采购计划", notes = "新增采购计划", response = PurchasePlanMasterVO.class)
	@PostMapping("/plan/add")
	@BusinessLog(value = "新增采购计划")
	@ApiImplicitParams({
	      @ApiImplicitParam(name = "name", value ="采购计划名称", dataType = "string", paramType = "query"),
	      @ApiImplicitParam(name = "type", value = "1：自动生成，2：手动添加", dataType = "int", paramType = "query")
	  })
	public ApiResult<?> addPurchasePlan(@RequestBody NameAndTypeDTO param){
		return ApiResult.success(toolPurchaseService.addPurchasePlan(param.getName(), param.getType()));
	}
	
	@ApiOperation(value = "新增编辑采购计划明细", notes = "新增编辑采购计划明细", response = ToolPurchaseDetailDTO.class)
	@PostMapping("/plan/detail")
	@BusinessLog(value = "新增编辑采购计划明细")
	public ApiResult<?> addPurchasePlanDetail(@RequestBody ToolPurchaseDetailDTO dto){
		toolPurchaseService.saveOrUpdatePlanDetail(dto);
		return ApiResult.success();
	}
	
	@ApiOperation(value = "验证是否有新增入库权限", notes = "验证是否有新增入库权限")
	@GetMapping("/permission/has")
	@BusinessLog("验证是否有新增入库权限")
	@RequiresPermissions("bs:tool:purchase:inwarehouse")
	public ApiResult<?> validateHasAddPermission(){
		commonBiz.sendCommandToDevice(MqttSendCommandFunc.COMMAND_2000.getValue());
		return ApiResult.success();
	}
	
	@ApiOperation(value = "增量入库归属信息", notes = "增量入库归属信息", response = CommonQueryResultVO.class)
	@GetMapping("/warehouse/belong")
	@BusinessLog(value = "增量入库归属信息")
	@RequiresPermissions("bs:tool:purchase:inwarehouse")
	public ApiResult<?> toolBelongToOrg(){
		return ApiResult.success(toolPurchaseService.toolBelongToOrg());
	}
	
	@ApiOperation(value = "入库主表-开始识别工器具", notes = "入库主表-开始识别工器具", response = PurchasePlanDetailVO.class)
	@GetMapping("/warehouse/master")
	@BusinessLog(value = "入库主表-开始识别工器具")
	@RequiresPermissions("bs:tool:purchase:inwarehouse")
	public ApiResult<?> inWarehouseMaster(ToolInWarehouseMasterDTO dto){
		toolPurchaseService.inWarehouseMaster(dto);
		return ApiResult.success();
	}
	
	@ApiOperation(value = "入库识别工器具结果", notes = "入库识别工器具结果", response=ToolInWarehouseDetailVO.class)
	@GetMapping("/warehouse/scan_result")
	@BusinessLog(value = "入库识别工器具结果")
	public ApiResult<?> toolScanResult(){
		return ApiResult.success(toolPurchaseService.toolScanResult());
	}
	
	@ApiOperation(value = "打印工器具编号", notes = "打印工器具编号", response=ApiResult.class)
	@GetMapping("/warehouse/print_code")
	@BusinessLog(value = "打印工器具编号")
	public ApiResult<?> printToolCode(@ApiParam(name = "code", value ="RFID", required = true) @RequestParam String code){
		toolPurchaseService.printToolCode(code);
		return ApiResult.success();
	}
	
	@ApiOperation(value = "继续入库", notes = "继续入库")
	@GetMapping("/warehouse/continue")
	@BusinessLog(value = "继续入库")
	public ApiResult<?> inWarehouseContinue(){
		CacheOperatorInfo.setMapValue(CacheKeyName.READER_LOG.getValue(), 
				CacheOperatorInfo.TOOL_CODE_READ_FLAG_ADD_NEW);
		CacheOperatorInfo.setMapValue(CacheKeyName.READER_CODE_LIST.getValue(), 
				CacheKeyName.READER_CODE_LIST.getValue());
		commonBiz.sendIvgScanStartCommand();
		return ApiResult.success();
	}
	
	@ApiOperation(value = "完成入库", notes = "完成入库", response=ApiResult.class)
	@GetMapping("/warehouse/done")
	@BusinessLog(value = "完成")
	public ApiResult<?> doneWarehouse(){
		return toolPurchaseService.submitInWarehouse();
	}
	
	@ApiOperation(value = "增量入库台账列表", notes = "增量入库台账列表", response = ToolInWarehouseStandingBookVO.class)
	@GetMapping("/warehouse/standing_book")
	@BusinessLog(value = "增量入库台账列表")
	@ApiImplicitParams({
	      @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
	      @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true),
	      @ApiImplicitParam(name = "orderBy", value = "排序字段", dataType = "string", paramType = "query"),
	      @ApiImplicitParam(name = "toolCode", value = "RFID", dataType = "string", paramType = "query"),
	      @ApiImplicitParam(name = "deviceCode", value = "柜子编号", dataType = "string", paramType = "query"),
	      @ApiImplicitParam(name = "typeModel", value = "类型型号", dataType = "string", paramType = "query"),
	      @ApiImplicitParam(name = "queryStartTime", value = "查询开始检", dataType = "string", paramType = "query"),
	      @ApiImplicitParam(name = "queryEndTime", value = "查询结束时间", dataType = "string", paramType = "query")
	  })
	@RequiresPermissions("bs:tool:purchase:inware:stand")
	public ApiResult<?> warehouseStandingBook(ToolBorrowReturnRecordDTO dto){
		return ApiResult.success(toolPurchaseService.warehouseStandingBook(dto));
	}
	
	@GetMapping("/warehouse/needAddToolList")
	@ApiOperation(value = "查询需要增量入库的工器具名称", response = CommonQueryResultVO.class)
	public ApiResult<List<CommonQueryResultVO>> needAddToolList(CommonQueryDTO queryParam) {
		return ApiResult.success(toolPurchaseService.needAddToolList(queryParam));
	}
	
	@GetMapping("/warehouse/needAddToolCode")
	@ApiOperation(value = "查询需要增量入库的工器具编号", response = CommonQueryResultVO.class)
	public ApiResult<List<CommonQueryResultVO>> needAddToolCode(CommonQueryDTO queryParam) {
		return ApiResult.success(toolPurchaseService.needAddToolCode(queryParam));
	}
	
}
